package day04.concurrent01;

/**
 * CountDownLatch 闭锁
 */
public class Demo03 {
    static int i = 3;
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    System.out.println("小刘去买米..");
                    synchronized (Demo03.class) {
                        i--;
                        if(i==0){
                            Demo03.class.notify();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("大刘去买菜..");
                    synchronized (Demo03.class) {
                        i--;
                        if(i==0){
                            Demo03.class.notify();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(4000);
                    System.out.println("老刘去买锅..");
                    synchronized (Demo03.class) {
                        i--;
                        if (i == 0) {
                            Demo03.class.notify();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        synchronized (Demo03.class){
            try {
                Demo03.class.wait();
                System.out.println("刘妈妈做饭..");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
